//
//  WQOneClickExecutionViewController.m
//  WisdomQuestor
//
//  Created by 纪景城 on 2024/12/18.
//

#import "WQOneClickExecutionViewController.h"
#import "WQSceneNormalCollectionCell.h"

@interface WQOneClickExecutionViewController ()

@property (nonatomic, strong) UIView           *filtrateView; // 全部筛选
@property (nonatomic, strong) UILabel           *filtrateTitleLabel;
@property (nonatomic, strong) UIImageView           *filtrateImageView;
@property (nonatomic, strong) UILabel           *filtrateNumsLabel;

@end

@implementation WQOneClickExecutionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.clearColor;
    
    [self.view addSubviews:[NSArray arrayWithObjects:self.filtrateView, nil]];
    [self.filtrateView addSubviews:[NSArray arrayWithObjects:self.filtrateTitleLabel, self.filtrateImageView, self.filtrateNumsLabel, nil]];
    [self fragmentUI_ViewSet];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((kScreen_Width - kFragmemtFloat(34)) / 2, kFragmemtFloat(116));
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    
    self.collectionView.collectionViewLayout = layout;
    self.collectionView.backgroundColor = UIColor.clearColor;
    
    [self.collectionView registerClass:[WQSceneNormalCollectionCell class] forCellWithReuseIdentifier:[WQSceneNormalCollectionCell kj_runtimeClassName]];
    
    [self collBeginRefresh];
}

- (UIView *)listView {
    return self.view;
}

#pragma mark ------- Http Mehods -------

- (void)requestHandlerWishIsRefresh:(BOOL)isRefresh {
    
    [[ThingSmartSceneManager sharedInstance] getSimpleSceneListWithHomeId:WQShare.userHomeModel.homeId success:^(NSArray<ThingSmartSceneModel *> *list) {
        
        DEBUGLOG(@"get simple scene list success = %@", list);
        self.collectionData = list;
        [self collEndRefresh];
        
    } failure:^(NSError *error) {
        DEBUGLOG(@"get simple scene list error = %@", error);
    }];
}

#pragma mark ------- UICollectionViewDelegate -------

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.collectionData.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    WQSceneNormalCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[WQSceneNormalCollectionCell kj_runtimeClassName] forIndexPath:indexPath];
    
    ThingSmartSceneModel *sceneModel = self.collectionData[indexPath.row];
    
    if (indexPath.row < 2) {
        cell.backgroundColor = [k_Color_BED7F3 kj_gradientAcrossToColor:k_Color_CCE3F3 width:(kScreen_Width - kFragmemtFloat(34)) / 2];
        [cell.moreThreeButton setImage:kImageName(@"scene_more_threedian") forState:0];
    } else {
        cell.backgroundColor = k_Color_D9ECD9;
        [cell.moreThreeButton setImage:kImageName(@"scene_more_threegreen") forState:0];
    }
    
    
    cell.sceneNameLabel.text = sceneModel.name;
    
    [cell.sceneTypeImageView ts_loadUrlString:sceneModel.conditions.firstObject.iconUrl];
//    [cell.sceneTypeImageView ts_loadUrlString:@"https://images.tuyacn.com/smart/rule/env/condition_60_new.png"];
    
    
    [cell.moreThreeButton kj_addAction:^(UIButton * _Nonnull kButton) {
        
    }];
    
    return cell;
}

#pragma mark ------- Lazy Methods -------

- (void)fragmentUI_ViewSet {
    [self.filtrateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(kFragmemtFloat(12));
        make.right.equalTo(self.view.mas_right).offset(-kFragmemtFloat(12));
        make.top.equalTo(self.view.mas_top).offset(kFragmemtFloat(10));
        make.height.mas_equalTo(kFragmemtFloat(60));
    }];
    [self.filtrateTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.filtrateView);
        make.left.equalTo(self.filtrateView.mas_left).offset(kFragmemtFloat(16));
    }];
    [self.filtrateImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.filtrateView);
        make.left.equalTo(self.filtrateTitleLabel.mas_right).offset(kFragmemtFloat(5));
        make.size.mas_equalTo(kSizzleKapadate(8, 5));
    }];
    [self.filtrateNumsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.filtrateView);
        make.right.equalTo(self.filtrateView.mas_right).offset(-kFragmemtFloat(18));
    }];
    [self.collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(kFragmemtFloat(12));
        make.right.equalTo(self.view.mas_right).offset(-kFragmemtFloat(12));
        make.top.equalTo(self.filtrateView.mas_bottom).offset(kFragmemtFloat(10));
        make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
    }];
}

- (UIView *)filtrateView {
    if (!_filtrateView) {
        _filtrateView = [[UIView alloc] init];
        _filtrateView.backgroundColor = UIColor.whiteColor;
        _filtrateView.cornerRadius = kFragmemtFloat(10);
    }
    return _filtrateView;
}

- (UILabel *)filtrateTitleLabel {
    if (!_filtrateTitleLabel) {
        _filtrateTitleLabel = [UILabel initWithTextColor:k_Color_222222 weight:UIFontWeightMedium size:14];
        _filtrateTitleLabel.text = @"全部筛选";
    }
    return _filtrateTitleLabel;
}

- (UIImageView *)filtrateImageView {
    if (!_filtrateImageView) {
        _filtrateImageView = [[UIImageView alloc] initWithImage:kImageName(@"scene_drop_down")];
    }
    return _filtrateImageView;
}

- (UILabel *)filtrateNumsLabel {
    if (!_filtrateNumsLabel) {
        _filtrateNumsLabel = [UILabel initWithTextColor:k_Color_666666 weight:UIFontWeightMedium size:14];
        _filtrateNumsLabel.text = @"(18个)";
    }
    return _filtrateNumsLabel;
}


@end
